/**
 * 内容系统 demo
 */

layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    //文章管理
    table.render({
        elem: '#LAY-app-content-list'
        , url: './res/json/content/list.js' //模拟接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: '文章ID', sort: true}
            , {field: 'label', title: '文章标签', minWidth: 100}
            , {field: 'title', title: '文章标题'}
            , {field: 'author', title: '作者'}
            , {field: 'uploadtime', title: '上传时间', sort: true}
            , {field: 'status', title: '发布状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
            , {title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //工具条
    table.on('tool(LAY-app-content-list)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此文章？', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑文章'
                , area: ['550px', '550px']
                , id: 'LAY-popup-content-edit'
                , success: function (layero, index) {
                    view(this.id).render('app/content/listform', data).done(function () {
                        form.render(null, 'layuiadmin-app-form-list');

                        //提交
                        form.on('submit(layuiadmin-app-form-submit)', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            //$.ajax({});
                            layui.table.reload('LAY-app-content-list'); //重载表格
                            layer.close(index); //执行关闭
                        });
                    });
                }
            });
        }
    });


    var tableElem = "LAY-home-tags-list";
    var formElem = "LAY-home-tags-listform";
    var tableToolsElem = "LAY-home-tags-tools";
    var formSubmitElem = "LAY-home-tags-listform-submit";
    //分类管理
    table.render({
        elem: `#${tableElem}`
        , url: window.globalConfig.url_label_list //模拟接口
        , cols: [[
            {type: 'numbers', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'label_name', title: '标签名', minWidth: 100}
            , {
                field: 'cms_id', title: '绑定的文章数量', templet: function (d) {
                    if (d.cms_id) {
                        return d.cms_id.split(",").length
                    } else {
                        return 0
                    }
                }
            }
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: `#${tableToolsElem}`}
        ]]
        , text: '对不起，加载出现异常！'
    });

    //工具条
    table.on(`tool(${tableElem})`, function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此标签？', function (index) {
                admin.req({
                    url: window.globalConfig.url_label_list + "/" + data.id,
                    type: "DELETE",
                    success: function (res) {
                        layer.msg(res.msg)
                        layer.close(index);
                        layui.table.reload(tableElem); //重载表格
                    }
                })
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑分类'
                , area: ['450px', '200px']
                , id: 'LAY-popup-content-tags'
                , success: function (layero, index) {
                    view(this.id).render('home/label/tagsform', data).done(function () {
                        form.render(null, formElem);

                        //提交
                        form.on(`submit(${formSubmitElem})`, function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            admin.req({
                                url: window.globalConfig.url_label_list + "/" + field.id,
                                type: "PUT",
                                data: field,
                                success: function (res) {
                                    layer.msg(res.msg)
                                    layui.table.reload(tableElem); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            })
                        });
                    });
                }
            });
        }
    });

    //评论管理
    table.render({
        elem: '#LAY-app-content-comm'
        , url: './res/json/content/comment.js' //模拟接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true}
            , {field: 'reviewers', title: '评论者', minWidth: 100}
            , {field: 'content', title: '评论内容', minWidth: 100}
            , {field: 'commtime', title: '评论时间', minWidth: 100, sort: true}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-content-com'}
        ]]
        , page: true
        , limit: 10
        , limits: [10, 15, 20, 25, 30]
        , text: '对不起，加载出现异常！'
    });

    //工具条
    table.on('tool(LAY-app-content-comm)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此条评论？', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑评论'
                , area: ['450px', '300px']
                , id: 'LAY-popup-content-comm'
                , success: function (layero, index) {
                    view(this.id).render('app/content/contform', data).done(function () {
                        form.render(null, 'layuiadmin-form-comment');

                        //提交
                        form.on('submit(layuiadmin-app-com-submit)', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            //$.ajax({});
                            layui.table.reload('LAY-app-content-comm'); //重载表格
                            layer.close(index); //执行关闭
                        });
                    });
                }
            });
        }
    });

    exports('contlist', {})
});